using System;
using System.Reflection;
using jSLCore.Client.Presentation.BaseControl;

namespace jSLApp.Client.Workflow
{
    public class ShowCase
    {
        private readonly string _name;
        private readonly string _category;
        private readonly ConstructorInfo _constructorInfo;
        private readonly int _order;
        private readonly bool _isDefault;

        public ShowCase(Type type)
        {
            var customAttribute = type.GetCustomAttributes(typeof(ShowCaseAttribute), false)[0] as ShowCaseAttribute;
            if (customAttribute == null) throw new ArgumentException("The type is not marked with ShowCaseAttribute");
            var name = customAttribute.Name;
            var category = customAttribute.Category;
            var order = customAttribute.Order;
            var isDefault = customAttribute.IsDefault;
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("Provide name for showcase");
            if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("Provide category for showcase");
            if (type == null) throw new ArgumentNullException("Provide type for showcase");
            if (!type.IsSubclassOf(typeof(BaseControl)))
                throw new ArgumentException("Type must be a subclass of BaseControl");
            var constructorInfo = type.GetConstructor(Type.EmptyTypes);
            if (constructorInfo == null) throw new ArgumentException("Type must provide constructor with out of parameters");
            _constructorInfo = constructorInfo;
            _name = name;
            _category = category;
            _order = order;
            _isDefault = isDefault;
        }

        public bool IsDefault
        {
            get { return _isDefault; }
        }

        public int Order
        {
            get { return _order; }
        }

        public string Category
        {
            get { return _category; }
        }

        public string Name
        {
            get { return _name; }
        }

        public BaseControl CreateInstance()
        {
            var control = _constructorInfo.Invoke(null);
            return (BaseControl)control;
        }
    }
}